home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: HELP! Modifying the EOF in a file!
- Date: Wed, 13 Mar 96 20:41:51 GMT
- Organization: none
- Message-ID: <826749711snz@genesis.demon.co.uk>
- References: <4i585k$4ia@news.netam.net> <4i58ki$uoh@grail.fgi.net>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4i58ki$uoh@grail.fgi.net>
- bwendlin@I_should_put_my_domain_in_etc_NNTP_INEWS_DOMAIN
- "Physics Lizard" writes:
-
- >The Bowling Green Connection inexplicably wrote:
- >} I am having trouble shortening the size of a text file...
- >} Suppose I have a text file with 3 lines of data, then I
- >} run this program:
- >
- >} void main() {
- >} FILE *dat;
- >} dat=fopen("file.txt", "r+");
- >} fprintf(dat, "Hello! %c", EOF);
- >} fclose(dat);
- >} }
- >
- >} Why doesn't the EOF character chop off the remaining two lines?
- >} When I print out the file after running this program, the ONLY thing
- >} that changed was the first few characters.. The other two lines
- >} still remain.. HELP!
-
- Because end-of-file is not a character in C, it is a condition or status
- a stream can enter. Certain input function like getchar() indicate this
- conditiion to the caller by returning the value of the EOF macro. The
- reason the getchar() returns int is because EOF is not necessarily
- a valid character value. Therefore trying to write it out as a character
- makes no sense.
-
- C provides no method of truncating a file at an arbitrary point. When you
- open a file using modes "w", "wb", "w+" or "w+b" then the file is truncated
- to zero length. To do what you want you typically create a second empty file
- and then copy as much as you need of the first file across to it.
-
- >You are opening the file read only. In order to do what you are
- >wanting, why don't you just open the file as write? Then, everything
- >in the file will be overwritten.
-
- No, the "r+" mode open the file for update i.e. both reading and writing.
- It doesn't truncate the file however.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-